package com.lianzai.wxapp.controller;

import com.alibaba.fastjson.JSONObject;
import com.lianzai.common.annotation.NoLoginAuthRequired;
import com.lianzai.common.config.WxConfig;
import com.lianzai.common.utils.R;
import com.lianzai.common.utils.StringUtils;
import com.lianzai.ticket.dao.TicketDao;
import com.lianzai.ticket.model.MerchantTicketDO;
import com.lianzai.ticket.model.TicketDiscount;
import com.lianzai.user.domain.UserInfoDO;
import com.lianzai.user.service.UserInfoService;
import com.lianzai.wxapp.service.MerchantService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Slf4j
@Controller
public class WxDebugController {

    @Autowired
    private WxConfig wxConfig;

    @Autowired
    private TicketDao ticketDao;

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 注册商户
     * @param
     * @return
     * @throws Exception
     */
    @NoLoginAuthRequired
    @RequestMapping(value ="/debug/ticket/merchant/pay", method = RequestMethod.POST)
    @ResponseBody
    public R debugBuyTicket(@RequestBody JSONObject json) throws Exception{

        String openId = json.getString("openId");
        if (StringUtils.isBlank(openId)) {
            return R.error("openId为空");
        }

        //获取用户信息
        UserInfoDO user = userInfoService.getByWxOpenId(json.getString("openId"));
        if (user == null){
            return R.error("用户未注册");
        }

        int ticketBuyNum = json.getInteger("ticketNum");
        if (ticketBuyNum <= 0) {
            return R.error("购买数量不能为0");
        }

        int totalTicketNum = ticketBuyNum;

        //计算是否有优惠，具体赠送的停车券数量
        List<TicketDiscount> discounts = ticketDao.getTicketDiscountByWXId(openId);
        if (discounts != null && discounts.size() > 0)
        {
            int gitNum = 0;

            /**
             * 循环分析各档优惠，以获取最大优惠为原则
             * 算法保证即便出现买得更多反而送得更少的异常规则，也能在所有规则范围内，为客户找到最大赠送值
             * 注：从数据库查询得到的优惠规则，是按购买数量进行了升序排序的
             */
            for (TicketDiscount discount : discounts)
            {
                if (ticketBuyNum < discount.getTicketBuyNum())
                {
                    break;  //优惠搜索结束
                }

                if (discount.getTicketGiftNum() > gitNum)
                {
                    gitNum = discount.getTicketGiftNum();
                }
            }

            totalTicketNum += gitNum;
        }

        MerchantTicketDO merchantTicketDO = new MerchantTicketDO();
        merchantTicketDO.setUserId(user.getId().intValue());
        merchantTicketDO.setTicketId(json.getInteger("ticketId"));
        merchantTicketDO.setTicketBuyedNum(totalTicketNum);
        merchantTicketDO.setTicketRemainNum(totalTicketNum);

        int ret = ticketDao.buyTicket(merchantTicketDO);

        if (ret == 1)
        {
            return R.ok("购买成功");
        }
        else
        {
            return R.error("购买失败");
        }
    }




}
